package cn.iocoder.yudao.module.blog.controller.app.post;


import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.blog.controller.admin.post.vo.BlogPostCreateReqVO;
import cn.iocoder.yudao.module.blog.controller.admin.post.vo.BlogPostPageReqVO;
import cn.iocoder.yudao.module.blog.controller.admin.post.vo.BlogPostRespVO;
import cn.iocoder.yudao.module.blog.controller.app.post.vo.AppBlogPostRespVO;
import cn.iocoder.yudao.module.blog.controller.app.post.vo.AppBlogTopReqVO;
import cn.iocoder.yudao.module.blog.convert.post.BlogPostConvert;
import cn.iocoder.yudao.module.blog.dal.dataobject.post.BlogPostDO;
import cn.iocoder.yudao.module.blog.dal.mysql.post.BlogPostMapper;
import cn.iocoder.yudao.module.blog.service.post.BlogPostService;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.List;
import java.util.Map;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

@Tag(name = "用户 APP - 帖子")
@RestController
@RequestMapping("/blog/post")
@Validated
@Slf4j
public class AppBlogPostController {
    @Resource
    private BlogPostService blogPostService;

    @Resource
    private MemberUserApi memberUserApi;

    @Resource
    private BlogPostMapper blogPostMapper;

    @PostMapping("/create")
    @Operation(summary = "创建帖子")
    public CommonResult<Long> createPost(@Valid @RequestBody BlogPostCreateReqVO createReqVO) {
        return success(blogPostService.createPost(createReqVO));
    }

    @GetMapping("/page")
    @Operation(summary = "获得帖子分页")
    public CommonResult<PageResult<AppBlogPostRespVO>> getPostPage(@Valid BlogPostPageReqVO pageVO) {
        PageResult<BlogPostDO> pageResult = blogPostService.getPostPage(pageVO);

        // 查询用户信息
        Set<Long> memberIds = convertSet(pageResult.getList(), BlogPostDO::getMemberId);
        Map<Long, MemberUserRespDTO> memberMap = memberUserApi.getUserMap(memberIds);

        return success(BlogPostConvert.INSTANCE.convertAppPage(pageResult, memberMap));
    }

    @GetMapping("/get")
    @Operation(summary = "获得帖子")
    @Parameter(name = "postId", description = "编号", required = true, example = "1024")
    public CommonResult<BlogPostRespVO> getPost(@RequestParam("postId") Long id) {
        BlogPostDO post = blogPostService.getPost(id);
        MemberUserRespDTO member = memberUserApi.getUser(post.getMemberId());

        return success(BlogPostConvert.INSTANCE.convertApp(post, member));
    }


    @PostMapping("/top")
    @Operation(summary = "获得置顶帖子")
    public CommonResult<List<AppBlogPostRespVO>> getTopList(@Valid @RequestBody AppBlogTopReqVO topReqVO){
        List<BlogPostDO> list = blogPostMapper.selectList(new LambdaQueryWrapper<BlogPostDO>()
                .eq(BlogPostDO::getMemberId, topReqVO.getModuleId())
                .eq(BlogPostDO::getIsTop, true));

        //查询用户信息
        Set<Long> memberIds = convertSet(list, BlogPostDO::getMemberId);
        Map<Long, MemberUserRespDTO> memberMap = memberUserApi.getUserMap(memberIds);

        return success(BlogPostConvert.INSTANCE.convertAppList(list, memberMap));
    }
}
